package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import clientgui.ValidatorGUI;

import exchange.Parser;

public class SocketReader implements Runnable {

	public SocketReader(Socket socket) {
		super();
		this.socket = socket;
	}

	private Socket socket;
	private ObjectInputStream in;
	Parser parser = new Parser();
	String message;

	@Override
	public void run() {
		try {
			in = new ObjectInputStream(socket.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

		while (true) {
			try {
				readMessage();
			} catch (IOException e) {
				System.out.println("Session is closed");
				try {
					in.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				break;
			}
		}
	}

	private String readMessage() throws IOException {
		try {
			message = (String) in.readObject();
			System.out.println("IN : " + message);
			String tip = ValidatorGUI.acceptER(message);
			ValidatorGUI.updateTip(tip);
			ValidatorGUI.refresh();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return message;
	}

}
